package com.softeem.xyblogmaster.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.xyblogmaster.domain.Articles;
import com.softeem.xyblogmaster.domain.Tags;
import com.softeem.xyblogmaster.service.ArticlesService;
import com.softeem.xyblogmaster.service.impl.TagsServiceImpl;
import com.softeem.xyblogmaster.util.AjaxResult;
import com.softeem.xyblogmaster.util.PageInfo;
import com.softeem.xyblogmaster.vo.ArticlesVO;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/articles")
public class ArticlesController {


    //先注入Service
    @Autowired
    private ArticlesService articlesService;

    @Autowired
    private TagsServiceImpl tagsService;

    //增加方法 http://localhost:8080/articles/create
    @PostMapping("/create")
    public AjaxResult doAdd(@RequestBody @Valid ArticlesVO articlesVO){
        Articles articles = new Articles();
        BeanUtil.copyProperties(articlesVO,articles);
        articlesService.saveAndRef(articles,articlesVO.getTags());
        return AjaxResult.ok("添加文章成功");
    }

    //http://localhost:8080/articles/remove/1
    @DeleteMapping("/remove/{aid}")
    public AjaxResult doRemove(@PathVariable("aid") int aid){
        articlesService.removeById(aid);
        return AjaxResult.ok("删除文章成功");
    }

    @PutMapping("/edit")
    public AjaxResult  doEdit(@RequestBody @Valid ArticlesVO articlesVO){
        Articles articles=new Articles();
        //只要属性名相同就会拷贝相同的属性的值 到另外一个对象中
        BeanUtil.copyProperties(articlesVO,articles);
        articlesService.saveAndRef(articles,articlesVO.getTags());
        return AjaxResult.ok("修改文章成功");
    }

    //http://localhost:8080/articles/detail/1
//    @GetMapping("/detail/{aid}")
//    public AjaxResult doDetail(@PathVariable("aid") @Min(1) int aid){
//        Articles byId = articlesService.getById(aid);
//
//            return AjaxResult.ok("查询成功",byId);
//
//    }
    //http://localhost:8080/articles/1
    //查询文章的同时 查询所有该文章关联的标签，最终把数据整合到ArticlesVO中返回到前台
    @GetMapping("/detail/{aid}")
    public AjaxResult  doDetail(@PathVariable("aid") @Min(1) int aid){
        Articles byId = articlesService.getById(aid);
        List<Tags> tags = tagsService.listTagsByArticleId(aid);
        Map resultMap=new HashMap();
        resultMap.put("article",byId);
        resultMap.put("tags",tags);
        return AjaxResult.ok("查询文章成功",resultMap);
    }

//    //http://localhost:8080/list/1/10
//    @GetMapping("/list/{page}/{limit}")
//    public AjaxResult doList(@PathVariable("page") int page,
//                             @PathVariable("limit") int limit,
//                             @RequestParam("keyword") String keyword){
//
//        LambdaQueryWrapper<Articles> lambdaQueryWrapper=new LambdaQueryWrapper();
//        lambdaQueryWrapper.like(Articles::getTitle,"%"+keyword+"%");
//        Page pages = articlesService.page(new Page<>(page,limit),lambdaQueryWrapper);
//        PageInfo pageInfo = new PageInfo(pages,7);
//        return AjaxResult.ok("查询成功",pageInfo);
//
//    }

    //http://localhost:8080/articles?page=1&limit=10&keyword=java
    @GetMapping("/list")
    public AjaxResult doList(@RequestParam(value = "page",defaultValue = "1") int page,
                             @RequestParam(value = "limit",defaultValue = "2") int limit,
                             @RequestParam(value = "keyword",defaultValue = "") String keyword)

    {
        PageInfo pageInfo = articlesService.page(page, limit, keyword);
        return AjaxResult.ok("查询文章成功",pageInfo);

    }

}
